using System;
using System.Collections;
using System.Data;
using BeeldBank.Business;
using BeeldBank.Business.Collection;

namespace BeeldBank.Business.Base
{
    /// <summary>
    /// Provides database independant methods for OrderRegel.
    /// </summary>
    public class OrderRegelBase : BusinessBase
    {
        internal OrderRegelBase(System.Data.DataRow row)
            : base(row)
        {
            this.OnPrepareForSave += new PrepareForSaveHandler(this.PrepareForSave);
        }

        public delegate void PrepareForSaveHandler();
        public event PrepareForSaveHandler OnPrepareForSave;

        #region property bool EnforceSave
        /// This property is used to indicate if the
        /// object in the save event should save, even
        /// if the object contents (marked as stereotype NOTENFORCESAVE) is not saved
        private bool _EnforceSave = false;
        public bool EnforceSave
        {
            get
            {
                return _EnforceSave;
            }
            set
            {
                _EnforceSave = value;
            }

        }
        #endregion

        #region PrepareForSaveBase()
        public void PrepareForSaveBase()
        {
            if (!this.PrepareForSaveFinished)
            {
                if (OnPrepareForSave != null)
                {
                    OnPrepareForSave();
                }
                this.CanLoad = false;
                // call prepareforsave on all dependant objects.
                if (this.Orders != null)
                {
                    this.Orders.PrepareForSaveBase();
                }
                if (this.Photo != null)
                {
                    this.Photo.PrepareForSaveBase();
                }

                this.CanLoad = true;
                this.PrepareForSaveFinished = true;
            }
        }
        #endregion

        #region PrepareForSave()
        public virtual void PrepareForSave()
        {
        }
        #endregion

        #region PreSave()
        public virtual void PreSave()
        {
        }
        #endregion

        #region Save()
        public void Save()
        {
            bool IsSaveNeeded = ContentChanged();

            if (IsDirty)
            {
                this.ModifiedBy = _luw.UserName;
                if (this.State == BusinessState.New)
                {
                    this.CreatedBy = _luw.UserName;
                    if (this.Orders != null)
                    {
                        this.Orders.Save();
                    }
                    if (this.Photo != null)
                    {
                        this.Photo.Save();
                    }
                    BeeldBank.Data.OrderRegel.Insert(this._dataRow, _luw.Transaction);

                }

                if ((this.State == BusinessState.Changed) && (IsSaveNeeded))
                {
                    BeeldBank.Data.OrderRegel.Update(this._dataRow, _luw.Transaction);
                }

                if (this.State == BusinessState.Deleted)
                {
                    BeeldBank.Data.OrderRegel.Delete(this._dataRow, _luw.Transaction);
                    if (this.Orders != null)
                    {
                        this.Orders.Save();
                    }
                    if (this.Photo != null)
                    {
                        this.Photo.Save();
                    }

                }
                IsDirty = false;
            }
        }
        #endregion

        #region OldOrderRegel
        private OrderRegel _oldOrderRegel;
        public OrderRegel OldOrderRegel
        {
            get
            {
                if (_oldOrderRegel == null && this.State != BusinessState.New)
                {
                    // Create the new object in a completely separate luw,
                    // otherwise a reference the the current object is returned.
                    Luw luw = new Luw(this.Luw.UserName);
                    _oldOrderRegel = luw.NewOrderRegel(BeeldBank.Data.OrderRegel.Open(this.Id).Rows[0]);
                }
                return _oldOrderRegel;
            }
        }
        #endregion

        #region Id
        /// <summary>
        /// 
        /// </summary>
        public int Id
        {
            get
            {
                return Convert.ToInt32(_dataRow["Id"]);
            }
            set
            {
                _dataRow["Id"] = value;
                this.IsDirty = true;
            }
        }
        public bool IdIsNull
        {
            get
            {
                return _dataRow["Id"] == Convert.DBNull;
            }
        }
        public void IdSetNull()
        {
            _dataRow["Id"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region OrderId
        /// <summary>
        /// Vreemde sleutel naar de order
        /// </summary>
        public int OrderId
        {
            get
            {
                return Convert.ToInt32(_dataRow["OrderId"]);
            }
            set
            {
                _dataRow["OrderId"] = value;
                this.IsDirty = true;
            }
        }
        public bool OrderIdIsNull
        {
            get
            {
                return _dataRow["OrderId"] == Convert.DBNull;
            }
        }
        public void OrderIdSetNull()
        {
            _dataRow["OrderId"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Type
        /// <summary>
        /// Het type waarin het bestand besteld is
        /// </summary>
        public string Type
        {
            get
            {
                return Convert.ToString(_dataRow["Type"]);
            }
            set
            {
                _dataRow["Type"] = value;
                this.IsDirty = true;
            }
        }
        public string TypeNaam
        {
            get
            {
                return Convert.ToString(_dataRow["TypeNaam"]);
            }
            set
            {
                _dataRow["TypeNaam"] = value;
                this.IsDirty = true;
            }
        }
        public bool TypeIsNull
        {
            get
            {
                return _dataRow["Type"] == Convert.DBNull;
            }
        }
        public void TypeSetNull()
        {
            _dataRow["Type"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Formaat
        /// <summary>
        /// Het formaat waarin het bestand besteld is
        /// </summary>
        public string Formaat
        {
            get
            {
                return (_dataRow["Formaat"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Formaat"]);
            }
            set
            {
                _dataRow["Formaat"] = value;
                this.IsDirty = true;
            }
        }
        public bool FormaatIsNull
        {
            get
            {
                return _dataRow["Formaat"] == Convert.DBNull;
            }
        }
        public void FormaatSetNull()
        {
            _dataRow["Formaat"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Afwerking
        /// <summary>
        /// De afwerking waarin een beeld besteld is
        /// </summary>
        public string Afwerking
        {
            get
            {
                return Convert.ToString(_dataRow["Afwerking"]);
            }
            set
            {
                _dataRow["Afwerking"] = value;
                this.IsDirty = true;
            }
        }
        public bool AfwerkingIsNull
        {
            get
            {
                return _dataRow["Afwerking"] == Convert.DBNull;
            }
        }
        public void AfwerkingSetNull()
        {
            _dataRow["Afwerking"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Aantal
        /// <summary>
        /// Aantal maal een media bestand besteld is
        /// </summary>
        public int Aantal
        {
            get
            {
                return (_dataRow["Aantal"] == Convert.DBNull) ? 0 : Convert.ToInt32(_dataRow["Aantal"]);
            }
            set
            {
                _dataRow["Aantal"] = value;
                this.IsDirty = true;
            }
        }
        public bool AantalIsNull
        {
            get
            {
                return _dataRow["Aantal"] == Convert.DBNull;
            }
        }
        public void AantalSetNull()
        {
            _dataRow["Aantal"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region MediaId
        /// <summary>
        /// Vreemde sleutel naar de media tabel
        /// </summary>
        public int MediaId
        {
            get
            {
                return Convert.ToInt32(_dataRow["MediaId"]);
            }
            set
            {
                _dataRow["MediaId"] = value;
                this.IsDirty = true;
            }
        }
        public bool MediaIdIsNull
        {
            get
            {
                return _dataRow["MediaId"] == Convert.DBNull;
            }
        }
        public void MediaIdSetNull()
        {
            _dataRow["MediaId"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region CreatedBy
        public String CreatedBy
        {
            get
            {
                return Convert.ToString(_dataRow["CreatedBy"]);
            }
            set
            {
                _dataRow["CreatedBy"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region CreatedAt
        public DateTime CreatedAt
        {
            get
            {
                return Convert.ToDateTime(_dataRow["CreatedAt"]);
            }
            set
            {
                _dataRow["CreatedAt"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region ModifiedBy
        public String ModifiedBy
        {
            get
            {
                return Convert.ToString(_dataRow["ModifiedBy"]);
            }
            set
            {
                _dataRow["ModifiedBy"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region ModifiedAt
        public DateTime ModifiedAt
        {
            get
            {
                return Convert.ToDateTime(_dataRow["ModifiedAt"]);
            }
            set
            {
                _dataRow["ModifiedAt"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region Version
        public int Version
        {
            get
            {
                return _dataRow["Version"] == Convert.DBNull ? -1 : Convert.ToInt32(_dataRow["Version"]);
            }
            set
            {
                _dataRow["Version"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region Orders
        private Order _orders;
        public Order Orders
        {
            get
            {
                if (_orders == null && _luw.CanLoad && this.CanLoad)
                {
                    System.Data.DataTable table = BeeldBank.Data.Order.Open(OrderId);
                    if (table.Rows.Count == 1)
                    {
                        _orders = _luw.NewOrder(table.Rows[0]);
                    }
                }
                return _orders;
            }
        }
        #endregion

        #region Photo
        private MediaObject _photo;
        public MediaObject Photo
        {
            get
            {
                if (_photo == null && _luw.CanLoad && this.CanLoad)
                {
                    System.Data.DataTable table = BeeldBank.Data.MediaObject.Open(MediaId);
                    if (table.Rows.Count == 1)
                    {
                        _photo = _luw.NewMediaObject(table.Rows[0]);
                    }
                }
                return _photo;
            }
        }
        #endregion

        #region ContentChanged
        /// <summary>
        /// ContentChanged
        /// This function checks whether the content of a record is changed, dependant on the "EnforceSave" 
        /// variable. If EnforceSave is false, all the fields which have not been marked "NOTENFORCINGSAVE" as
        /// a stereotype in the design are checked. The contents of the fields are checked against the old values
        /// if they are not equal,the result is true. If the contents are equal, the result is false.
        /// 
        /// If EnforceSave is true, the result is always true.
        /// </summary>
        /// <returns>boolean indicating whether the Content is Changed</returns>
        public bool ContentChanged()
        {
            // Change enforced by Class Variables
            if (this.EnforceSave) { return (true); }
            if (this.IdIsNull) { return (true); }

            // Change enforced by the attributes

            #region Id
            /// Id is excluded because it is the primary key
            #endregion

            #region OrderId

            if (this.OrderIdIsNull != this.OldOrderRegel.OrderIdIsNull) { return (true); }
            if (this.OrderId != this.OldOrderRegel.OrderId) { return (true); }

            #endregion

            #region Type

            if (this.TypeIsNull != this.OldOrderRegel.TypeIsNull) { return (true); }
            if (this.Type != this.OldOrderRegel.Type) { return (true); }

            #endregion

            #region Formaat

            if (this.FormaatIsNull != this.OldOrderRegel.FormaatIsNull) { return (true); }
            if (this.Formaat != this.OldOrderRegel.Formaat) { return (true); }

            #endregion

            #region Afwerking

            if (this.AfwerkingIsNull != this.OldOrderRegel.AfwerkingIsNull) { return (true); }
            if (this.Afwerking != this.OldOrderRegel.Afwerking) { return (true); }

            #endregion

            #region Aantal

            if (this.AantalIsNull != this.OldOrderRegel.AantalIsNull) { return (true); }
            if (this.Aantal != this.OldOrderRegel.Aantal) { return (true); }

            #endregion

            #region MediaId

            if (this.MediaIdIsNull != this.OldOrderRegel.MediaIdIsNull) { return (true); }
            if (this.MediaId != this.OldOrderRegel.MediaId) { return (true); }

            #endregion


            return false;
        }
        #endregion

        #region WhereAmI
        /// <summary>
        /// Provides the WhereAmI display text for a OrderRegel with the specified id
        /// </summary>
        /// <param name="id">Id of the OrderRegel.</param>
        /// <return>>WhereAmI display text</returns>
        public static string WhereAmI(int id)
        {
            string whereAmI = "";
            if (id == 0)
            {
                whereAmI = "New OrderRegel";
            }
            else
            {
                DataTable table = BeeldBank.Data.OrderRegel.Show(id);

            }
            return whereAmI;
        }
        #endregion

    }
}
